<?php
/**
 * 《拔打电话》实现类
 *
 * @author 孙晓晔
 * @version $Id: Rate.class.php 804 2009-12-30 10:05:41Z sunxy $
 */
require_once(dirname(__FILE__) . '/TelUtil.class.php');

class Call extends TelUtil {
	public $tbl = Constant::tbl_call;
	
	public $call = array(
		'-' => '请选择',
		'1' => '电信线路',
		'2' => '网络电话',
		'3' => '内部电话',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'exten' => '分机',
		'number' => '号码',
		'call' => '线路',

		'1' => '创建时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('fixed_option', $this->fixed);		
		$controller->assign_by_ref('call_option', $this->call);
	}

	/**
	 * 拔打电话
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$s = $request['number'];
		$call = $this->_call_number($s);
		
		$controller->assign_by_ref('call', $call);

		$controller->display($request, 'tel/call/call');
		return true;
	}
	
	function _call_number($s) {
		if (preg_match("/^(2|6|8)\d{3}$/i", $s)) {
			return '3';
		}
		
		if (preg_match("/^(\+|00)/i", $s)) {
			return '2';
		}
		
		return '1';
	}

	/**
	 * 拔打电话
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$exten = $request['exten'];
		$number = $request['number'];

		$call = $request['call'];
		if (empty($call) || $call == '-') {
			$call = "1";
		}

		$db = newdb('emp');
		$fmt = "INSERT INTO %s(exten, number, `call`, createtime) VALUES('%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $exten, $number, $call);
		debug($sql);
		$db->query($sql);

		$number = $this->_destination($number, $call);

		$aa['Action'] = 'OriginateCall';
		$aa['Type'] = 'sunxy';
		$aa['Source'] = $exten;
		$aa['CallerID'] = $exten;
		$aa['Destination'] = $number;

		$this->_call($aa);
		return true;
	}

	function _destination($s, $call) {
		if (preg_match('/^(2|6|8)\d{3}$/', $s)) {
			return $s;
		}
		
		if (preg_match('/^6\d{2,5}$/', $s)) {
			return $s;
		}

		if (preg_match('/^(\\+|-|\s|\d|\\(0\\))+$/i', $s)) {
			$pattern = array('/\\+/', '/-/', '/\s/', '/\\(0\\)/');
			$replacement = array('00', '', '', '');
			$s = preg_replace($pattern, $replacement, $s);
		}

		if (preg_match('/^0086755/', $s)) {
			return '9' . substr($s, 7);
		}

		if (preg_match('/^0755/', $s)) {
			return '9' . substr($s, 4);
		}

		if (preg_match('/^0086/', $s)) {
			if ($call == '1') {
				return '917909' . substr($s, 4);
			}
			
			return $s;
		}

		if (preg_match('/^00/', $s)) {
			if ($call == '1') {
				return '9' . $s;
			}
			
			return $s;
		}
			
		if (preg_match('/^0/', $s)) {
			if ($call == '1') {
				return '917909' . $s;
			}
		}

		if ($call == '1') {
			return '9' . $s;
		}

		return $s;
	}

	function _call($aa) {
		$exten = $aa['Source'];

		if (preg_match('/^2\d{3}$/', $exten)) {
			$sip = 'qx';
		}

		if (preg_match('/^6\d{3}$/', $exten)) {
			$sip = 'ns';
		}

		if (preg_match('/^8\d{3}$/', $exten)) {
			$sip = 'sj';
		}

		if (empty($sip)) {
			return '';
		}
		
		$am = astman($sip);
		
		$channel = 'SIP/' . $exten;
		$am->Originate($channel, $aa['Destination'], 'sunxy', '1', NULL, NULL, 8000, $aa['CallerID'], NULL, NULL, NULL, NULL);

		$am->disconnect();
	}

	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'tel/call/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$exten_s = $request['exten_s'];
		$number_s = $request['number_s'];
		$call_s = $request['call_s'];

		$where = " WHERE 1=1";

		if (!empty($call_s) && $call_s <> '-') {
			$where .= " AND call = '" . $call_s . "'";
		}

		if (!empty($exten_s)) {
			$where .= " AND exten = '" . $exten_s . "'";
		}
		
		if (!empty($number_s)) {
			$where .= " AND number LIKE '" . $number_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'tel/call/list');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		if (preg_match("/^(2|6|8)\d{3}$/", $row['number'])) {
			$row['call'] = 3;
		}
		
		$row['src_t'] = $this->_number($row['exten']);
		$row['dst_t'] = $this->_number($row['number']);

		$row['call_t'] = $this->call[$row['call']];
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function dh($controller, $request) {
		$bb = $this->_list_email('343');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '电话检测';
		
		$request['ast'] = 'dh';
		$reqeust['sec'] = 600;
		
		$data = $this->_abc($controller, $request);
		if (empty($data)) {
			return;
		}
		
		$controller->assign_by_ref('result', $data);
		
		$body = $this->_tj_html($controller, $request, 'tel/call/tj_a');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function fax($controller, $request) {
		$bb = $this->_list_email('343');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '传真检测';
		
		$request['ast'] = 'fax';
		$reqeust['sec'] = 300;
		
		$data = $this->_abc($controller, $request);
		if (empty($data)) {
			return;
		}
		
		$controller->assign_by_ref('result', $data);
		
		$body = $this->_tj_html($controller, $request, 'tel/call/tj_a');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function qx($controller, $request) {
		$bb = $this->_list_email('343');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
	
		$from = $this->email;
		$subject = '清溪电话';
	
		$request['ast'] = 'qx';
		$reqeust['sec'] = 3600;
	
		$data = $this->_abc($controller, $request);
		if (empty($data)) {
			return;
		}
	
		$controller->assign_by_ref('result', $data);
	
		$body = $this->_tj_html($controller, $request, 'tel/call/tj_a');
	
		$this->_tj_mail($from, $bb, $subject, $body);
	
		logdate($controller, $request);
		return true;
	}
	
	function ns($controller, $request) {
		$bb = $this->_list_email('343');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '南山电话';
		
		$request['ast'] = 'ns';
		$reqeust['sec'] = 3600;
		
		$data = $this->_abc($controller, $request);
		if (empty($data)) {
			return;
		}
		
		$controller->assign_by_ref('result', $data);
		
		$body = $this->_tj_html($controller, $request, 'tel/call/tj_a');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function sj($controller, $request) {
		$bb = $this->_list_email('343');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '沙井电话';
		
		$request['ast'] = 'sj';
		$reqeust['sec'] = 1800;
		
		$data = $this->_abc($controller, $request);
		if (empty($data)) {
			return;
		}
		
		$controller->assign_by_ref('result', $data);
		
		$body = $this->_tj_html($controller, $request, 'tel/call/tj_a');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function fy($controller, $request) {
		$bb = $this->_list_email('343');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '电话检测';
		
		$request['ast'] = 'fy';
		
		$data = $this->_abc($controller, $request);
		if (empty($data)) {
			return;
		}
		
		$controller->assign_by_ref('result', $data);
		
		$body = $this->_tj_html($controller, $request, 'tel/call/tj_a');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _abc($controller, $request) {
		$min_sec = $request['min_sec'];
		if (empty($min_sec)) {
			$min_sec = 300;
		}
		
		$max_sec = $request['sec'];
		if (empty($max_sec)) {
			$max_sec = 1200;
		}
				
		$am = astman($request['ast']);		
		$cmd = 'core show channels';
		$ret = $am->Command($cmd);

		$data = $ret['data'];
		// echo $data;
		$aa = preg_split("/[\\n]+/i", $data);
		
		$i = 0;
		$ret = array();
		while (list($k, $s) = each($aa)) {
			echo sprintf("%s\n", $s);
			
			$i++;			
			if ($i < 3) {
				continue;
			}
						
			if (preg_match("/(\d+) active channels/i", $s, $m)) {
				continue;
			}
			
			if (preg_match("/(\d+) active call/i", $s, $m)) {
				break;
			}
			
			$a = preg_split("/[\s,]+/", $s);
			$channel = $a[0];			
			// print_r($a);
			
			$row = $this->_channel($am, $channel);
			if (preg_match("/^(DAHDI|Zap)/i", $row['channel']) && preg_match("/^(DAHDI|Zap)/i", $row['dstchannel'])) {
				echo "Hangup...\n";
				$am->Hangup($channel);
				continue;
			}
			
			if (empty($row['billsec']) || $row['billsec'] < $min_sec) {
				continue;
			}

			/*
			if (preg_match("/^(2|6|8)\d{3}$/i", $row['src'])) {
				$sec = $this->_timeout($row['src']);			
				if ($row['billsec'] > $sec) {					
					echo "Hangup...\n";
					print_r($row);
					
					$am->Hangup($channel);
					continue;
				}
			} else {
				$sec = $request['sec'];
				if (empty($sec)) {
					$sec = 1200;
				}
				
				if ($row['billsec'] > $max_sec) {
					echo "Hangup...\n";
					$am->Hangup($channel);
					continue;
				}
			}*/			
			
			$row['billsec_t'] = $this->printtime($row['billsec']);
			
			$row['src_o'] = $this->_number($row['src']);
			$row['dst_o'] = $this->_number($row['dst']);
			
			$ret[] = $row;
		}
		
		$am->disconnect();
		
		return $ret;
	}
	
	function _timeout($exten) {
		require_once(dirname(__FILE__) . '/../emp/Exten.class.php');
		$aa = new Exten();
		
		return $aa->_timeout($exten);
	}
	
	function _channel($am, $chn) {
		$cmd = sprintf("core show channel %s", $chn);
		// echo $cmd . "\n";
		$ret = $am->Command($cmd);

		$data = $ret['data'];
		// echo $data;
		
		$aa = preg_split("/[\\n]+/i", $data);
		
		$i = 0;
		$ret = array();
		while (list($k, $s) = each($aa)) {
			if (preg_match("/level 1: (\\S+)=(.+)/i", $s, $m)) {
				// print_r($m);
				$ret[$m[1]] = $m[2];
			}
		}		
		
		return $ret;
	}

	function t($controller, $request) {

		print_r($request);
		
		logdate($controller, $request);
		return true;
	}

	function z($controller, $request) {
		$exten = '6595';
		$number = '13760120595';
		$call = "2";

		$number = $this->_destination($number, $call);

		$aa['Action'] = 'OriginateCall';
		$aa['Type'] = 'tt';
		$aa['Source'] = $exten;
		$aa['CallerID'] = $exten;
		$aa['Destination'] = $number;

		$this->_call($aa);
		return true;
	}
}
?>
